/////////////////////////////////////////////////////////////////////////////
// Name:        ax_activator.cpp
// Purpose:     Standard activator classes
// Author:      Julian Smart
// Modified by:
// Created:     2009-03-20
// RCS-ID:
// Copyright:   (c) Julian Smart
// Licence:     New BSD License
/////////////////////////////////////////////////////////////////////////////

#include "wx/wx.h"

#include "ax/ax_view.h"
#include "ax/ax_menu_choice.h"
#include "ax/ax_activator.h"
#include "ax/ax_validator.h"

IMPLEMENT_CLASS( AxActivator, wxEvtHandler )

// Called when the user selects an item, for example to describe the item
bool AxActivator::Select(AxMenuController* controller, AxMenuItem* item)
{
    if (controller && controller->GetViewManager() && controller->GetViewManager()->GetDescriptionCtrl())
    {
        controller->GetViewManager()->GetDescriptionCtrl()->SetValue(item->GetDescription());
    }
    return true;
}

// Called just before the menu item is displayed.
bool AxActivator::BeforeDisplay(AxMenuController* WXUNUSED(controller), AxMenuItem* WXUNUSED(item))
{
    return false;
}

// Called during display, at intervals.
bool AxActivator::UpdateUI(AxMenuController* WXUNUSED(controller), AxMenuItem* WXUNUSED(item))
{
    return false;
}

IMPLEMENT_DYNAMIC_CLASS( AxEventActivator, AxActivator )

void AxEventActivator::Copy(const AxEventActivator& activator)
{
    m_menuItem = activator.m_menuItem;
    m_eventId = activator.m_eventId;
    m_sink = activator.m_sink;
}

AxMenu* AxEventActivator::Activate(AxMenuController* WXUNUSED(controller), AxMenuItem* item)
{
    if (m_sink)
    {
        // TODO: use special event with menu returned in it
        wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED, m_eventId);
        event.SetEventObject(item);

        m_sink->ProcessEvent(event);
    }
    return NULL;
}

/*
 * AxUpActivator implementation.
 * Goes up a level.
 */

IMPLEMENT_DYNAMIC_CLASS( AxUpActivator, AxActivator )

void AxUpActivator::Copy(const AxUpActivator& activator)
{
    AxActivator::Copy(activator);
    //m_controller = activator.m_controller;
}

AxMenu* AxUpActivator::Activate(AxMenuController* controller, AxMenuItem* WXUNUSED(item))
{
    if (controller)
        controller->GoUp();
    return NULL;
}

/*
 * AxAutoUpdateActivator implementation.
 */

IMPLEMENT_CLASS( AxAutoUpdateActivator, AxActivator )

AxAutoUpdateActivator::AxAutoUpdateActivator(
        const wxString& labelSpec,
        const wxString& descriptionSpec)
{
    Init();

    m_labelSpec = labelSpec;
    m_descriptionSpec = descriptionSpec;
}

void AxAutoUpdateActivator::Init()
{
}

void AxAutoUpdateActivator::Copy(const AxAutoUpdateActivator& activator)
{
    m_labelSpec = activator.m_labelSpec;
    m_descriptionSpec = activator.m_descriptionSpec;
}

// Called just before the menu item is displayed.
bool AxAutoUpdateActivator::BeforeDisplay(AxMenuController* WXUNUSED(controller), AxMenuItem* item)
{
    UpdateItem(NULL, item);

    return true;
}

// Called during display, at intervals.
bool AxAutoUpdateActivator::UpdateUI(AxMenuController* controller, AxMenuItem* item)
{
    UpdateItem(controller, item);

    return true;
}

// Update label or description
bool AxAutoUpdateActivator::UpdateItem(AxMenuController* controller, AxMenuItem* item)
{
    if (item)
        item->TransferDataToMenu();

    wxString label = CreateString(m_labelSpec, true);
    wxString descr = CreateString(m_descriptionSpec, false);
    bool changed = false;
    if (!label.IsEmpty() && item && item->GetLabel() != label)
    {
        item->SetLabel(label);
        changed = true;
    }
    if (!descr.IsEmpty() && item && item->GetDescription() != descr)
    {
        item->SetDescription(descr);
        changed = true;
    }
    
    if (changed && controller)
    {
        controller->UpdateMenuItem(item);
    }

    return true;
}
